<template>
  <div class="edit-tools">
    <el-tooltip
      v-for="item in btnList"
      :key="item.key"
      :content="item.name"
      effect="light"
      placement="left"
    >
      <el-button
        circle
        class="btn-item"
        :icon="item.icon"
        @click="item.handle"
      />
    </el-tooltip>
  </div>
</template>

<script>
import throttle from 'lodash/throttle'
import mixins from '../../mixins/update'
import { getLocalStorage } from '@/utils'
import { mapGetters, mapMutations } from 'vuex'
import { fetchPathByName } from '@/utils'
export default {
  mixins: [mixins],
  computed: {
    ...mapGetters(['getStorageInfo', 'editCanvasConfig', 'componentList'])
  },
  data () {
    return {
      btnList: [
        {
          key: 'import',
          type: 'importUpload',
          name: '导入',
          icon: 'el-font-import',
          handle: 'import'
        },
        {
          key: 'export',
          type: 'button',
          name: '导出',
          icon: 'el-font-export',
          handle: 'exportHandle'
        },
        {
          key: 'edit',
          type: 'button',
          name: '编辑',
          icon: 'el-font-edit',
          handle: this.editHandle
        }
      ]
    }
  },
  methods: {
    ...mapMutations('chartEdit', ['setEditCanvas']),

    // 编辑处理
    editHandle () {
      this.$message.warning('请通过顶部【同步内容】按钮同步最新数据！')
      this.setEditCanvas({ key: 'isCodeEdit', value: true })
      setTimeout(() => {
        // 获取id路径
        const path = fetchPathByName('ChartEdit', 'href')
        if (!path) return
        this.updateToSession()
        let { href } = this.$router.resolve({ path: '/edit' })
        window.open(href, '_blank')
      }, 2000)
    },
    // 把内存中的数据同步到SessionStorage 便于传递给新窗口初始化数据
    updateToSession (id) {
      let storageInfo = { editCanvasConfig: this.editCanvasConfig, componentList: this.componentList }
      const sessionStorageInfo = getLocalStorage('chartList') || []
      sessionStorage.setItem('chartList', JSON.stringify([{ ...storageInfo }]))
    },
    // 同步数据到预览页
    SyncUpdateHandle () {
      window.addEventListener('blur', () => {
        let storageInfo = { editCanvasConfig: this.editCanvasConfig, componentList: this.componentList }
        dispatchEvent(new CustomEvent('preview', { detail: storageInfo }))
      })
    },
    // 更新处理
    updateFn (e) {
      this.$message.success('正在进行更新...')
      this.updateComponent(e.detail, true)
    }
  },
  watch: {
    '$route.name': {
      handler (toName, fromName) {
        if (fromName == 'home') {
          removeEventListener('blur', this.SyncUpdateHandle)
          removeEventListener('SaveJSON', this.updateFn)
        }
        if (toName == 'home') {
          // 失焦同步数据
          addEventListener('blur', this.SyncUpdateHandle)

          // 监听编辑器保存事件 刷新工作台图表
          addEventListener('SaveJSON', this.updateFn)

          // 监听编辑页关闭
          addEventListener('close', throttle(() => this.setEditCanvas({ key: 'isCodeEdit', value: false }), 1000))
        }
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
.edit-tools {
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  border-radius: 25px;
  border: 1px solid #e5e6eb;
  height: auto;
  padding: 30px 5px;
  right: 30px;
  bottom: 50px;
  overflow: hidden;
  transition: height ease 0.4s;
  background-color: rgba(240, 240, 240, 0.7);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(20px);
  .btn-item {
    margin-left: 0;
    margin-bottom: 10px;
  }
}
::v-deep .el-button:hover {
  border-color: #47e0c4;
  background-color: #fff;
}
</style>